<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Ferry_Config extends Stourweb_Controller
{
    private $_typeid = 105;

    public function before()
    {
        parent::before();
        $action = $this->request->action();
        //这里需要补权限的判断功能
    }

    public function action_index()
    {
        $config = DB::select()->from('sysconfig')
            ->where('varname','like','cfg_plugin_ferry_%')
            ->or_where('varname','=','cfg_ferry_supplier_check_auto')
            ->execute()
            ->as_array();
        $list = array();
        foreach($config as $v)
        {
            $list[$v['varname']] = $v['value'];
        }
        $this->assign('list',$list);
        $this->display('admin/ferry/config/index');
    }

    public function action_ajax_config_save()
    {
        $cfg_arr = array('webid'=>0);
        foreach($_POST as $k=>$v)
        {
            if(strpos($k,'cfg_')===0)
            {
                $cfg_arr[$k] = $v;
            }
        }
        $sys_model = new Model_Sysconfig();
        $sys_model->saveConfig($cfg_arr);
        echo json_encode(array('status'=>true,'msg'=>'保存成功'));

    }
    public function action_agreement()
    {
        $config = DB::select()->from('sysconfig')->where('varname','like','cfg_plugin_ferry_agreement_%')->execute()->as_array();
        $list = array();
        foreach($config as $v)
        {
            $list[$v['varname']] = $v['value'];
        }
        $this->assign('list',$list);
        $this->display('admin/ferry/config/agreement');
    }

    public function action_notice()
    {
        $data = array(
            'cfg_ferry_notice_open',
            'cfg_ferry_notice',
        );
        $cfg = array();
        foreach ($data as $v){
            $cfg[$v] = DB::select()->from('sysconfig')->where('varname','=',$v)->execute()->get('value');
        }

        $this->assign('cfg',$cfg);
        $this->display('admin/ferry/config/notice');
    }

    public function action_ajax_notice_save()
    {
        $data = array(
            'cfg_ferry_notice_open' => Arr::get($_POST,'cfg_ferry_notice_open'),
            'cfg_ferry_notice'      => Arr::get($_POST,'cfg_ferry_notice'),
        );
        foreach ($data as $k=>$v){
            $is_exist = DB::select()->from('sysconfig')->where('varname','=',$k)->execute()->current();
            if($is_exist){
                DB::update('sysconfig')->set(array('value'=>$v))->where("id={$is_exist['id']}")->execute();
            }else{
                $arr = array(
                    'webid' => 0,
                    'varname' => $k,
                    'info' => '景区公告',
                    'value' => $v,
                );
                DB::insert('sysconfig', array_keys($arr))->values(array_values($arr))->execute();
            }
        }

        echo json_encode(array('status'=>true,'msg'=>'保存成功'));
    }
}